<div ng-controller="manageC">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">管理员工</h1>
    </div>
    <div class="wrapper-md">
        <div class="panel panel-default">
            <div class="table-responsive">
                <form name="form" class="form-validation">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <span class="h4">编辑 <em class="text-muted"> {{datauser.Name}} </em> 信息</span>
                        </div>
                        <div class="panel-body">
                            <div class="form-group">
                                <label>设置权限</label>
                                <div class="checkbox">
                                    <label class="i-checks" ng-repeat="item in lists">
                                        <input type="checkbox" ng-checked="item.isCheck" name="roles" value="{{item.value}}" ng-click="check(item)">
                                        <i></i>
                                        {{item.text}}
                                    </label>
                                </div>
                            </div>
                    </div>
                        <footer class="panel-footer text-right bg-light lter">
                            <button type="button" class="btn btn-warning" ng-click="resetpwd()">重置密码</button>
                            <button type="button" class="btn btn-success" ng-disabled="form.$invalid" ng-click="managebtn()">提交</button>
                            <button type="button" class="btn btn-default" ng-click="abolish()">取消</button>
                        </footer>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    angular.module('app')
        //管理员工控制器
    .controller("manageC", function ($scope, $state, $stateParams, $http) {

        $scope.id = JSON.parse($stateParams.userdata);
        //获取数据
        $http({
            url: "../api/Employee/Detail?id=" + $scope.id,
            method: "GET"
        })
          .then(function (res) {
              $scope.datauser = res.data;
              $scope.lists = [
                  { id: "a", value: "A", text: "管理员", isCheck: false },
                  { id: "b", value: "1", text: "客户专员", isCheck: false },
                  { id: "c", value: "2", text: "运营", isCheck: false },
                  { id: "d", value: "3", text: "审批人", isCheck: false },
                  { id: "e", value: "4", text: "财务人员", isCheck: false },
                  { id: "f", value: "5", text: "客服专员", isCheck: false },
                  { id: "g", value: "6", text: "驻厂人员", isCheck: false },
                  { id: "h", value: "7", text: "采购专员", isCheck: false }
              ];
              if ($scope.datauser.Roles) {
                  for (var item1 in $scope.lists) {
                      var a = $scope.datauser.Roles.indexOf($scope.lists[item1].value) >= 0;
                      $scope.lists[item1].isCheck = a;
                  }
              }
          })
          .then(function (error) {
              if (error) {
                  console.log("error");
              }
          });
        $scope.check = function (item) {
            item.isCheck = !item.isCheck;
        };
        //提交新数据
        $scope.managebtn = function () {
            $scope.arr = [];
            for (var item1 in $scope.lists) {
                if ($scope.lists[item1].isCheck == true) {
                    $scope.arr.push($scope.lists[item1].value);
                }
            }
            $http.put("/api/Admin/SetRoles?id=" + $scope.id, $scope.arr)
                .success(function () {
                    alert("提交成功");
                    $state.go("app.Employee_List");
                });
        };
        //返回上一页
        $scope.abolish = function () {
            $state.go('app.Employee_List');
        };
        //重置密码
        $scope.resetpwd = function () {
            $scope.tan = confirm("确定重置密码？");
            if ($scope.tan == true) {
                $http({
                    url: "../api/Admin/ResetPwd?id=" + $scope.id,
                    method: "PUT"
                })
                    .then(function (res) {
                        alert("重置密码成功");
                        $state.go("app.Employee_List");
                    })
                    .then(function (error) {
                        if (error) {
                            console.log("error");
                        }
                    });
            } else {
                console.log("操作取消");
            }
        }
    })
</script>